//
//  XJYMyHeadView.m
//  ShopMall
//
//  Created by XJY on 17/1/18.
//  Copyright © 2017年 XinJinYing. All rights reserved.
//

#import "XJYMyHeadView.h"

@interface XJYMyHeadView()

//头部视图背景
@property (nonatomic, strong) UIImageView *headImageView;
//登录按钮
@property (nonatomic, strong) UIButton *loginBtn;
//注册按钮
@property (nonatomic, strong) UIButton *registBtn;

//用户头像
@property (nonatomic, strong) UIImageView *iconView;
//用户名称
@property (nonatomic, strong) UILabel *userNameLabel;

@end

@implementation XJYMyHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.headImageView];
        [self addSubview:self.loginBtn];
        [self addSubview:self.registBtn];
        [self addSubview:self.iconView];
        [self addSubview:self.userNameLabel];
    }
    return self;
}


#pragma mark  - 懒加载
- (UIImageView *)headImageView
{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"我的背景"]];
    }
    return _headImageView;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:18.0f];
        [_loginBtn.titleLabel setTextColor:[UIColor whiteColor]];
        [_loginBtn addTarget:self action:@selector(pushLoginView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

- (UIButton *)registBtn
{
    if (!_registBtn) {
        _registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
        _registBtn.titleLabel.font = [UIFont systemFontOfSize:18.0f];
        [_registBtn.titleLabel setTextColor:[UIColor whiteColor]];
        [_registBtn addTarget:self action:@selector(pushLandingView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registBtn;
}

- (UIImageView *)iconView
{
    if (!_iconView) {
        _iconView = [[UIImageView alloc] init];
        _iconView.backgroundColor = [UIColor yellowColor];
        _iconView.layer.cornerRadius =  30;
        _iconView.layer.masksToBounds = YES;
    }
    return _iconView;
}

- (UILabel *)userNameLabel
{
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc] init];
        _userNameLabel.text = @"辛金应";
    }
    return _userNameLabel;
}

#pragma mark - 自动布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    __weak typeof(self) weakSelf = self;
    [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.edges.equalTo(weakSelf);
        make.edges.equalTo(weakSelf).with.insets(UIEdgeInsetsMake(0, 0, 40, 0));
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX).offset(-60);
        make.centerY.equalTo(weakSelf.headImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 23));
    }];
    
    [_registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX).offset(60);
        make.centerY.equalTo(weakSelf.headImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 23));
    }];
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.headImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.left.equalTo(weakSelf.headImageView.mas_left).offset(40);
    }];
    
    [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.iconView.mas_top);
        make.left.equalTo(weakSelf.iconView.mas_right).offset(20);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
}


#pragma mark - 点击事件
- (void)pushLandingView:(UIButton *)button
{
    if (_landingBlock) {
        _landingBlock();
    }
}

- (void)pushLoginView:(UIButton *)button
{
    if (_loginBlock) {
        _loginBlock();
    }
}

#pragma mark - 刷新头部页面
- (void)reloadHeadView
{
    NSNumber *flag = [[NSUserDefaults standardUserDefaults] objectForKey:@"ISLOGIN"];
    if (flag) {
        _loginBtn.hidden = YES;
        _registBtn.hidden = YES;
        _iconView.hidden = NO;
        _userNameLabel.hidden = NO;
    } else {
        _loginBtn.hidden = NO;
        _registBtn.hidden = NO;
        _iconView.hidden = YES;
        _userNameLabel.hidden = YES;
    }
}

@end
